package com.huizhonglm.diap.ui.user;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.core.image.Thumb;
import com.huizhonglm.diap.core.storage.model.ClaimShopInfo;
import com.huizhonglm.diap.core.utils.LogUtil;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.core.zxing.CaptureActivity;
import com.huizhonglm.diap.presenters.user.ClaimShopPresenter;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.shop.ShopDetailActivity;
import com.huizhonglm.diap.ui.user.iView.ClaimShopIView;
import com.huizhonglm.diap.widget.RoundRectImageView;

/**
 *
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-22 13:42
 */
public class ClaimShopActivity extends BaseActivity implements ClaimShopIView, View.OnClickListener {
    private final int REQ_CODE_CAPTURE_QR_CODE = 1;

    private TextView saoyisaoTxt;
    private View shopLayout;
    private RoundRectImageView shopLogoImg;
    private TextView shopNameTxt;
    private TextView shopDescTxt;

    private ClaimShopInfo shop;
    private ClaimShopPresenter mPresenter;

    private String userID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_claim_shop);
        setBackEvent(this, null);
        userID = UserUtil.getUserId();
        mPresenter = new ClaimShopPresenter(this);
        assignViews();
        showProgressBar();
        mPresenter.getClaimShop(userID);
    }

    @Override
    protected void assignViews() {
        shopLogoImg = (RoundRectImageView) findViewById(R.id.shop_logo_img);
        shopNameTxt = (TextView) findViewById(R.id.shop_name_txt);
        shopDescTxt = (TextView) findViewById(R.id.shop_desc_txt);
        saoyisaoTxt = (TextView) findViewById(R.id.saoyisao_txt);
        shopLayout = findViewById(R.id.shop_layout);

        saoyisaoTxt.setOnClickListener(this);
        shopLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.saoyisao_txt:
                LogUtil.print("shop_capture", "--- saoyisao_txt ---");
                Intent intent = new Intent();
                intent.setClass(this, CaptureActivity.class);
                startActivityForResult(intent, REQ_CODE_CAPTURE_QR_CODE);
                break;
            case R.id.shop_layout:
                if (shop != null) {
                    Intent detail = new Intent(this, ShopDetailActivity.class);
                    detail.putExtra("shopId", shop.getShopId());
                    startActivity(detail);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        LogUtil.print("shop_capture", "--- url - " + data.getStringExtra("result"));
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQ_CODE_CAPTURE_QR_CODE:
                    Uri uri = Uri.parse(data.getStringExtra("result"));
                    String shopId = uri.getLastPathSegment();
                    LogUtil.print("shop_capture", "--- shopId - " + shopId);
                    if (!EmptyUtil.isEmpty(shopId)) {
                        showProgressBar();
                        mPresenter.claimShop(userID, shopId);
                    }
                    break;
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onClaimShopSuccess(ClaimShopInfo info) {
        hideProgressBar();
        if (info != null && !EmptyUtil.isEmpty(info.getShopName())) {
            shop = info;
            saoyisaoTxt.setVisibility(View.GONE);
            shopLayout.setVisibility(View.VISIBLE);
            Thumb.getInstance().display(shopLogoImg, info.getShopLogo(), R.drawable.def_logo);
            shopNameTxt.setText(info.getShopName());
            shopDescTxt.setText(info.getShopDesc());
        } else {
            saoyisaoTxt.setVisibility(View.VISIBLE);
            shopLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClaimShopFailure() {
        hideProgressBar();
        saoyisaoTxt.setVisibility(View.VISIBLE);
        shopLayout.setVisibility(View.GONE);
    }
}
